package mud.support;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class Parser {
    private Document doc;

    public void connectUrl(String url) {
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getTitle() {
        if (doc == null)
            return "error";
        else
            return doc.title();
    }

    public String getName() {
        if (doc == null)
            return "error";

        Elements elements = doc.getElementsByClass("info");
        Element item = elements.first().child(0);
        if (item.tagName().equals("h1")) {
            return item.text();
        } else
            return "error";
    }

    public String getEd2k() {
        if (doc == null)
            return "error";

        Elements elements = doc.getElementsByClass("played2k");
        Element item = elements.first().child(0);
        if (item.tagName().equals("textarea")) {
            return item.text();
        } else
            return "error";
    }
}
